﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DemoObject;
using BusinessObjects;

public partial class Admin_Controls_MenuAE : System.Web.UI.UserControl
{
    public Guid MenuId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["MenuId"] != null)
        {
            MenuId = new Guid(Request["MenuId"].ToString());
            btnUpdate.Text = "Cập nhật";
        }
        else MenuId = Guid.Empty;
        if (!IsPostBack)
        {
            LoadDDL();
            LoadData();
        }

    }
    void LoadDDL()
    {
        HashSet<string> hs = new HashSet<string>();
        hs.Add("Name ASC");
        ddlMenuType.DataSource = TblMenuTypeRepository.CreateInstant().SelectListItem(null, "", "", hs);
        ddlMenuType.DataTextField = "Name";
        ddlMenuType.DataValueField = "Id";
        ddlMenuType.DataBind();
    }
    private void LoadData()
    {
        if (!MenuId.Equals(Guid.Empty))
        {
            TblMenu item = TblMenuRepository.CreateInstant().SelectOne(MenuId);
            txtName.Text = item.Name;
            ddlMenuType.SelectedValue = item.MenuTypeId.ToString();
            if (item.ParentId.HasValue)
                ddlParent.SelectedValue = item.ParentId.Value.ToString();
            ddlMenuType.SelectedValue = item.MenuTypeId.ToString();
            txtLinkUrl.Text = item.LinkUrl;
            cbIsVisible.Checked = item.IsVisible;
            lblCreatedBy.Text = item.CreatedBy;
            lblCreatedDate.Text = item.CreatedDate.ToString("dd/MM/yyyy");
            lblUpdatedBy.Text = item.UpdatedBy;
            lblUpdatedDate.Text = item.UpdatedDate.ToString("dd/MM/yyyy");
        }
    }
    private TblMenu GetTblMenu()
    {
        TblMenu obj = new TblMenuRepository().SelectOne(MenuId);
        if (obj == null)
        {
            obj = new TblMenu();
            obj.Id = Guid.NewGuid();
            obj.CreatedBy = HttpContext.Current.User.Identity.Name;
            obj.CreatedDate = DateTime.Now;
            obj.DisplayOrder = 100;
        }
        if (ddlParent.SelectedValue.Equals(string.Empty))
            obj.ParentId = null;
        else obj.ParentId = new Guid(ddlParent.SelectedValue);
        obj.MenuTypeId = new Guid(ddlMenuType.SelectedValue);
        obj.LinkUrl = txtLinkUrl.Text;
        obj.Name = txtName.Text;
        obj.IsVisible = cbIsVisible.Checked;
        obj.UpdatedBy = HttpContext.Current.User.Identity.Name;
        obj.UpdatedDate = DateTime.Now;

        return obj;
    }


    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (MenuId.Equals(Guid.Empty))
            new TblMenuRepository().Insert(GetTblMenu());
        else
            new TblMenuRepository().Update(GetTblMenu());
        Response.Redirect("MenuList.aspx");
    }
}
